<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            min-height: 100vh;
            background-color: #f5f5f5;
        }
        .sidebar {
            background-color: #ff5000;
            color: white;
            padding: 15px;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            width: 250px;
            min-height: 100vh;
        }
        .sidebar h1 {
            margin: 0 0 20px 0;
        }
        .menu {
            display: flex;
            flex-direction: column;
            gap: 20px;
            width: 100%;
        }
        .menu a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
            width: 100%;
            box-sizing: border-box;
        }
        .menu a:hover {
            background-color: #e04a00;
        }
        .content {
            flex-grow: 1;
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            margin: 20px;
            border-radius: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        table, th, td {
            border: 1px solid #ccc;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f5f5f5;
        }
        .form-group {
            display: inline-block;
            margin-right: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input, .form-group select {
            width: auto;
            padding: 5px;
            margin-bottom: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .form-group button {
            padding: 10px 20px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .form-group button:hover {
            background-color: #555;
        }
        .actions {
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 10px;
            margin: 20px;
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
        }
        .actions button {
            padding: 10px 20px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .actions button:hover {
            background-color: #555;
        }
        .search-form {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            gap: 15px;
        }
    </style>
</head>
<body>
<div class="sidebar">
    <h1>商品管理</h1>
    <div class="menu">
        <a href="admin">管理员主页</a>
        <a href="adminproductcategories">商品类型管理</a>
        <a href="adminorder">订单管理</a>
        <a href="adminusers">用户管理</a>
        <a href="login1">登出</a>
    </div>
</div>

<div class="content">
    <h2>商品管理</h2>
    <div class="actions">
        <button onclick="showProductList()">商品列表</button>
        <button onclick="showAddProduct()">新增商品</button>
    </div>
    <br>
    <div class="form-group">
        <label for="searchName">商品名称</label>
        <input type="text" id="searchName" name="name">
    </div>
    <div class="form-group">
        <label for="minPrice">最低价格</label>
        <input type="number" id="minPrice" name="minPrice" min="0" step="0.01">
    </div>
    <div class="form-group">
        <label for="maxPrice">最高价格</label>
        <input type="number" id="maxPrice" name="maxPrice" min="0" step="0.01">
    </div>
    <div class="form-group">
        <label for="status">上架状态</label>
        <select id="status" name="status">
            <option value="">全部</option>
            <option value="1">待售</option>
            <option value="2">在售</option>
            <option value="3">停售</option>
        </select>
    </div>
    <div class="form-group">
        <label for="isHot">热销状态</label>
        <select id="isHot" name="isHot">
            <option value="">全部</option>
            <option value="1">热销</option>
            <option value="2">一般</option>
        </select>
    </div>
    <button onclick="filterProductList()">查询</button>
    <div id="moduleContent"></div>
</div>

<script>
    let allProducts = []; // 用于存储所有商品数据

    function showAddProduct() {
        window.location.href = `productAdd`;
    }

    function showProductList() {
        fetch('/products', {
            method: 'GET'
        })
            .then(response => response.json())
            .then(products => {
                allProducts = products; // 保存所有商品数据
                renderProductList(products);
            })
            .catch(error => console.error('Error fetching product data:', error));
    }

    function renderProductList(products) {
        let productRows = products.map(product => {
            let statusButton = '';
            if (product.status == '1') {
                statusText1 = '待售';
                statusButton = `<button onclick="toggleProductStatus(${product.id})">上架</button>`;
            } else if (product.status=="2") {
                statusText1 = '在售';
                statusButton = `<button onclick="toggleProductStatus(${product.id})">下架</button>`;
            } else {
                statusText1 = '停售';
                statusButton = `<button onclick="toggleProductStatus(${product.id})">开售</button>`;
            }

            let hotButton = '';
            if (product.isHot == '1') {
                statusText = '热销';
                hotButton = `<button onclick="markAsBestseller(${product.id})">取消热销</button>`;
            } else {
                statusText = '一般';
                hotButton = `<button onclick="markAsBestseller(${product.id})">设为热销商品</button>`;
            }

            return `
            <tr>
                <td>${product.name}</td>
                <td>${product.price}</td>
                <td>${statusText1}${statusButton}</td>
                <td>${statusText}${hotButton}</td>
                <td>
                    <button onclick="editProduct(${product.id})">编辑</button>
                </td>
            </tr>
        `;
        }).join('');

        document.getElementById('moduleContent').innerHTML = `
        <h3>商品列表</h3>
        <table>
            <thead>
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>状态</th>
                    <th>是否热销</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                ${productRows}
            </tbody>
        </table>
        `;
    }

    function editProduct(id) {
        window.location.href = `productUpdate?productId=${id}`;
    }

    function toggleProductStatus(id) {
        fetch(`/products/${id}/toggle-status`, {
            method: 'PUT'
        })
            .then(response => {
                if (response.ok) {
                    showProductList();
                }
            })
            .catch(error => console.error('Error toggling product status:', error));
    }

    function markAsBestseller(id) {
        fetch(`/products/${id}/toggle-hot`, {
            method: 'PUT'
        })
            .then(response => {
                if (response.ok) {
                    showProductList();
                }
            })
            .catch(error => console.error('Error toggling product hot status:', error));
    }

    function filterProductList() {
        const searchName = document.getElementById('searchName').value.toLowerCase();
        const minPrice = parseFloat(document.getElementById('minPrice').value) || 0;
        const maxPrice = parseFloat(document.getElementById('maxPrice').value) || Number.MAX_VALUE;
        const status = document.getElementById('status').value;
        const isHot = document.getElementById('isHot').value;

        const filteredProducts = allProducts.filter(product => {
            return (
                product.name.toLowerCase().includes(searchName) &&
                product.price >= minPrice &&
                product.price <= maxPrice &&
                (status === '' || product.status == status) &&
                (isHot === '' || product.isHot == isHot)
            );
        });

        renderProductList(filteredProducts);
    }

    document.addEventListener('DOMContentLoaded', showProductList);
</script>
</body>
</html>
